package com.zhiyou.controller.LogisticsController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.Logistics;
import com.zhiyou.service.SystemLogisticsService.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/logistics")
public class LogisticsController {

    @Autowired
    LogisticsService logisticsService;

    @RequestMapping("/all")
    public String getAll(@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        PageHelper.startPage(pn,5);
        List<Logistics> list = logisticsService.getAll();
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "logilist";
    }

    @RequestMapping("/detail")
    public String detail(String loId, Map<String ,Logistics> map){
        int id = Integer.parseInt(loId);
        //System.out.println("logistics    logidetail   ");
        Logistics logistics = logisticsService.detail(id);
        System.out.println(logistics.toString());
        map.put("logis1",logistics);
        return "logidetail";
    }
    @RequestMapping("/find")
    public String find(String loId, Map<String ,Logistics> map){
        int id = Integer.parseInt(loId);
        Logistics logistics = logisticsService.detail(id);
        map.put("logis2",logistics);
        return "logiedit";
    }

    @RequestMapping("/edit")
    public String edit(Logistics logistics,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        System.out.println(logistics.toString());
        int i = logisticsService.update(logistics);
        if (i==1){
            PageHelper.startPage(pn,20);
            List<Logistics> list = logisticsService.getAll();

            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "logilist";
        }
        return "logiedit";
    }

    @RequestMapping("/add")
    public String addOne(Logistics logistics,String myTime,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(myTime);
        try {
            Date parse = ft.parse(myTime);
            logistics.setLoTime(parse);
            logistics.setLoState(0);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int i = logisticsService.addOne(logistics);
        if (i==1){
            PageHelper.startPage(pn,20);
            List<Logistics> list = logisticsService.getAll();

            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "logilist";
        }
        return "logiadd";
    }
    @RequestMapping("/kong")
    public String kong(){
        return "logiadd";
    }

    @RequestMapping("/delete")
    public String deleteOne(String loId,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        int id = Integer.parseInt(loId);
        int i = logisticsService.deleteOne(id);

        PageHelper.startPage(pn,20);
        List<Logistics> list = logisticsService.getAll();

        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "logilist";

    }
    @RequestMapping("/search")
    public String search(String field,String keyword,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map,HttpSession session){
        System.out.println("field = " + field + ", keyword = " + keyword);
        List<Logistics> list = null;
        if (keyword == ""){
            session.removeAttribute("field");
            session.removeAttribute("keyword");
            PageHelper.startPage(pn,5);
            list = logisticsService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "logilist";
        }
        session.setAttribute("field",field);
        session.setAttribute("keyword",keyword);
        PageHelper.startPage(pn,5);
        list = logisticsService.search(field,keyword);
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo1",pageInfo);
        return "logilist2";
    }


    @RequestMapping("/shouye")
    public String shouye(@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        PageHelper.startPage(pn,5);
        List<Logistics> list = logisticsService.getAll();
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "logilist";
    }

}